package cn.stylefeng.guns.modular.rms.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.rms.entity.Outtask;
import cn.stylefeng.guns.modular.rms.mapper.OuttaskMapper;
import cn.stylefeng.guns.modular.rms.model.params.OuttaskParam;
import cn.stylefeng.guns.modular.rms.model.result.OuttaskResult;
import  cn.stylefeng.guns.modular.rms.service.OuttaskService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 外包任务 服务实现类
 * </p>
 *
 * @author yzb
 * @since 2022-04-28
 */
@Service
public class OuttaskServiceImpl extends ServiceImpl<OuttaskMapper, Outtask> implements OuttaskService {

    @Override
    public void add(OuttaskParam param){
        Outtask entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(OuttaskParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(OuttaskParam param){
        //判断ID是否存在，如果不存在，则新建
        if (param.getId()!=null){
            Outtask oldEntity = getOldEntity(param);
            Outtask newEntity = getEntity(param);
            ToolUtil.copyProperties(newEntity, oldEntity);
            this.updateById(newEntity);
        }else{
            this.add(param);
        }
    }

    @Override
    public OuttaskResult findBySpec(OuttaskParam param){
        return this.baseMapper.findBySpec(param);
    }

    @Override
    public List<OuttaskResult> findListBySpec(OuttaskParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(OuttaskParam param){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    private Serializable getKey(OuttaskParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private Outtask getOldEntity(OuttaskParam param) {
        return this.getById(getKey(param));
    }

    private Outtask getEntity(OuttaskParam param) {
        Outtask entity = new Outtask();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
